package awt.awt05;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class graphics {
    private final String RECT_SHAPE="rect";
    private final String OVAL_SHAPE="oval";

    private Frame fr=new Frame("测试绘图");

    Button btnRect=new Button("绘制矩形");
    Button btnOval=new Button("绘制椭圆");


    private String shape="";

    private class MyCanvas extends Canvas{
        @Override
        public void paint(Graphics g){
            if(shape.equals(RECT_SHAPE)){
                // 绘制矩形
                // 设置当前画笔的颜色为黑色
                g.setColor(Color.black);
                g.drawRect(100,100,150,150);

            }else if(shape.equals(OVAL_SHAPE)){
                // 绘制椭圆
                 g.setColor(Color.red);
                 g.drawOval(100,100,150,100);
            }
        }
    }


    // 创建自定义画布对象
    MyCanvas drawArea=new MyCanvas();


    public void init(){

        btnRect.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                shape=RECT_SHAPE;
                drawArea.repaint();
            }
        });

        btnOval.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                shape=OVAL_SHAPE;
                drawArea.repaint();
            }
        });


        Panel p=new Panel();

        p.add(btnRect);
        p.add(btnOval);

        // 设置画布大小
        drawArea.setPreferredSize(new Dimension(300,300));

        fr.add(drawArea,BorderLayout.NORTH);
        fr.add(p);

        fr.pack();
        fr.setVisible(true);

    }

    public static void main(String[] args){
        new graphics().init();
    }

}
